const router = {
	'www.555kyy.com': '.dplayer-web-fullscreen-fix .fixedGroup, .dplayer-web-fullscreen-fix .sidebar, .dplayer-web-fullscreen-fix .header, .dplayer-web-fullscreen-fix .module-player-side{display:none!important} .dplayer:fullscreen video{height:100%!important} .MacPlayer, :not(.dplayer-fulled) video{height:600px!important}',
	//'www.ourxh.com': '.dplayer-web-fullscreen-fix #player-sidebar-is, .dplayer-web-fullscreen-fix .myui-panel_bd{display:none!important}',
	'acgndmku.com': '.dplayer-web-fullscreen-fix .hot_banner, #fd_tips, .dplayer-web-fullscreen-fix .foot ul.extra{display:none!important} #topnav{position: absolute!important}',
	// 'ke.qq.com': '.study-video-wrapper--gray:after{display:none!important}',
	'v.qq.com': '.dplayer-web-fullscreen-fix #mod_player~*, .dplayer-web-fullscreen-fix #shortcut, .dplayer-web-fullscreen-fix .site_head{display:none!important}',
	'wetv.vip': '.gm-fp-body .play__aside--right, .gm-fp-body .sidebar, .gm-fp-body header{display:none!important}'
};
// router['www.bashiyy.com'] = router['www.gdzidong.com'] = router['www.ysjdm.net'] = router['acgndmku.com'];
router['www.555dsv.com'] = router['www.555kyy.com'];
const ss = router[host];

const inRange = (n, min, max) => Math.max(min, n) == Math.min(n, max);
const reNf = /^https:\/\/\w+\.bigmao\.top/;
const handleMessage = async(v, url, vType) => {
	if (!v || v.offsetHeight < 55 || v.offsetWidth < 211) return;
	if (reNf.test(url)) return;
	if (typeof ss == 'string') dom.style({}, ss);
	else if (typeof ss == 'function') ss();

	let p;
	if (host == 'www.yatu.tv') {
		p = q('.dplayer');
		if (p) p.innerHTML = '';
		else {
			p = document.createElement('div');
			p.setAttribute('style', 'width:100%;height:97%;');
			v.after(p);

			v.parentNode.style.height = '100%';
			const e = q('.play_xian');
			e && e.addEventListener('click', ev => {
				if (ev.target.matches('a')) v.hidden = !1;
			});
		}
		v.src = '';
		v.matches('iframe') ? v.hidden = true : v.remove();
	}
	else if (/eacg\.net|wsdy\.top/.test(host)) {
		p = q('.mo-head-info');
		if (p) {
			p.classList.remove('mo-part-fixs');
			p.style.cssText = 'position: absolute';
		}
		p = v.parentNode;
		p.innerHTML = p.className = '';
		p.style.cssText = 'width:100%;height:80vh;';
	}
	else if (host == 'ke.qq.com') {
		p = v.parentNode;
		p.innerHTML = '<div style="width:100%;height:100%"></div>';
		p.classList.remove('study-video-wrapper--gray');
		p = p.firstChild;
	}
	else {
		if (!ss && v.closest('.MacPlayer td')) dom.style({}, router['acgndmku.com']);

		await sleep(333);
		p = document.createElement('div');
		const w = v.offsetWidth;
		let h = v.offsetHeight;
		if (!inRange(h, w*0.4, w*0.75)) {
			c = v.closest('div[class]');
			// if (!c) h = v.parentNode.offsetHeight;
			if (!c || !inRange(c.offsetHeight, w*0.4, w*0.75)) {
				h = w*0.6;
				if (c) c.style.height = h + 'px';
			}
		}
		// const h = Math.max(w * 0.4, v.offsetHeight);
		// const s = v.matches(':only-child') ? `width:100%;height:100%` : `width:${w}px;height:${h}px`;
		p.setAttribute('style', `width:100%;height:${h}px`);
		v.replaceWith(p);
	}
	while (!window.DPlayer) await sleep(99); //防止hls等库后于DPlayer库加载
	createPlayer(p, url, vType);
};

window.addEventListener("message", ev => {
	let w = ev.source;
	if (!w || !ev.data.url || ev.data.id !== 'metal-h5-get-frame') return;
	while (w.parent != top) w = w.parent;
	const v = find(e => e.contentWindow == w);
	handleMessage(v, ev.data.url, ev.data.vType);
	chrome.runtime.sendMessage({id:'inject-lib',vType:ev.data.vType, url: ev.data.url});
}, false);

chrome.runtime.onMessage.addListener((msg, sender) => {
	let v = null;
	switch (msg.id) {
	case 'mv-block':
		v = [].find.call(document.querySelectorAll('object,embed'), isMVFlash);
		break;
	case 'iframe-block':
		v = msg.frameUrl && find(e => e.src == msg.frameUrl);
	}
	handleMessage(v, msg.url, msg.vType || '');
});